package com.mingqijia.gassafety.console.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import com.mingqijia.gassafety.shared.constant.OperateRecordEnum;
import com.mingqijia.gassafety.shared.constant.PageColumnConst;
import com.mingqijia.gassafety.shared.log.LogRecord;
import com.mingqijia.gassafety.shared.utils.HeaderUtil;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.*;
import com.mingqijia.gassafety.webserver.request.runningReport.RunningReportEditPreviewRequest;
import com.mingqijia.gassafety.webserver.response.*;
import com.mingqijia.gassafety.webserver.response.runningReport.RunningReportDeviceListResponse;
import com.mingqijia.gassafety.webserver.response.runningReport.RunningReportPreviewResponse;
import com.mingqijia.gassafety.webserver.service.*;
import com.mingqijia.gassafety.webserver.service.impl.XFCallServiceImpl;
import com.mingqijia.gassafety.webserver.service.workorder.mbp.MbpWorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import static com.mingqijia.gassafety.shared.constant.PageColumnConst.*;


/**
 * <p>
 * 运行报告管理
 *
 * @author mby
 * @since 2024-03-11
 */

@Slf4j
@RestController
@RequestMapping("/business/v1/runningReport")
@Api(tags = "运行报告管理")
public class RunningReportController {

    @Autowired
    RunningReportService runningReportService;

    @Autowired
    XFCallServiceImpl xfCallService;

    @Autowired
    ConsumerService consumerService;

    @Autowired
    ConsumerServiceInfoService consumerServiceInfoService;

    @Autowired
    HeaderUtil headerUtil;

    @Autowired
    DtuMapper dMapper;
    @Autowired
    EquipmentService equipmentService;

    @Value(value = "${gassafety.installationPosition.checkSwitch}")
    private boolean checkSwitch = true;

    @Autowired
    CustomerSubsHistoryService customerSubsHistoryService;

    @Autowired
    MbpWorkOrderService mbpWorkOrderService;

    @Autowired
    XFCallServiceImpl callService;
    @Autowired
    OrganizationService organizationService;

    @Autowired
    FieldListShowService fieldListShowService;
    @Autowired
    ContactsService contactsService;

    @ApiOperation(value = "运行报告管理tab页")
    @PostMapping("/tab")
    public CommonResponse<List<StateCountResponse>> tab(@RequestBody RunningReportRequest request) {
        return CommonResponse.success(runningReportService.runningReportTab(request));
    }

    @ApiOperation(value = "canGenerate 查看是否可全部重新生成 0-可以 1-不可以")
    @PostMapping("/canGenerate")
    public CommonResponse<Integer> canGenerate(@RequestParam(value = "reportDate") String reportDate) {
        // 所有报告都未审核可以重新生成
        return CommonResponse.success(runningReportService.canGenerate(reportDate));
    }

    @ApiOperation(value = "canGenerate 查看是否可全部下载 0-可以 1-不可以")
    @PostMapping("/canDownload")
    public CommonResponse<Integer> canDownload(@RequestParam(value = "reportDate") String reportDate) {
        // 所有报告都不在数据更新中可以全部下载
        return CommonResponse.success(runningReportService.canDownload(reportDate));
    }

    @ApiOperation(value = "运行报告管理列表")
    @PostMapping("/list")
    public CommonResponse<PageUtil<RunningReportResponse>> list(@RequestBody RunningReportRequest request) {
        PageUtil<RunningReportResponse> runningReportList = runningReportService.runningReportList(request);
        runningReportList.setColumns(PageColumnConst.add(runningReportListTitles, runningReportListIndices));
        return CommonResponse.success(runningReportList);
    }

    @ApiOperation(value = "用户类型列表")
    @GetMapping("/getCustTypeList")
    public CommonResponse<List<String>> getCustTypeList() {
        return CommonResponse.success(runningReportService.getCustTypeList());
    }

    @ApiOperation(value = "运行报告详情")
    @GetMapping("/detail")
    public CommonResponse<RunningReportDetailResponse> detail(@RequestParam("id") @NotNull Integer id) {
        return CommonResponse.success(runningReportService.detail(id));
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_63)
    @ApiOperation(value = "更新数据")
    @PatchMapping("/updateData")
    public CommonResponse<Object> updateData(@RequestParam(value = "id") Integer id) {
        runningReportService.updateData(id);
        return CommonResponse.success("success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_69)
    @ApiOperation(value = "批量更新数据")
    @PutMapping("/batchUpdateData")
    public CommonResponse<Object> batchUpdateData(@RequestParam(value = "ids") List<Integer> ids) {
        return CommonResponse.success(runningReportService.batchUpdateData(ids), "success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_68)
    @ApiOperation(value = "全部更新数据")
    @PutMapping("/allUpdateData")
    public CommonResponse<Object> allUpdateData(@RequestParam(value = "reportDate") String reportDate) {
        return CommonResponse.success(runningReportService.allUpdateData(reportDate), "success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_64)
    @ApiOperation(value = "审核")
    @PatchMapping("/check")
    public CommonResponse<Object> check(@RequestParam(value = "id") Integer id) {
        runningReportService.check(id);
        return CommonResponse.success("success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_67)
    @ApiOperation(value = "批量审核")
    @PutMapping("/batchCheck")
    public CommonResponse<Object> batchCheck(@RequestParam(value = "ids") List<Integer> ids) {
        return CommonResponse.success(runningReportService.batchCheck(ids), "success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_66)
    @ApiOperation(value = "全部审核")
    @PutMapping("/allCheck")
    public CommonResponse<Object> allCheck(@RequestParam(value = "reportDate") String reportDate) {
        return CommonResponse.success(runningReportService.allCheck(reportDate), "success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_65)
    @ApiOperation(value = "下载")
    @GetMapping("/download")
    public CommonResponse<Object> download(@RequestParam(value = "id") Integer id) {
        runningReportService.download(id);
        return CommonResponse.success("success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_71)
    @ApiOperation(value = "批量下载")
    @PostMapping("/batchDownload")
    public CommonResponse<Object> batchDownload(@RequestParam(value = "ids") List<Integer> ids) {
        runningReportService.batchDownload(ids);
        return CommonResponse.success("success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_72)
    @ApiOperation(value = "全部下载")
    @GetMapping("/allDownload")
    public CommonResponse<Object> allDownload(@RequestParam(value = "reportDate") String reportDate) {
        runningReportService.allDownload(reportDate);
        return CommonResponse.success("success");
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_70)
    @ApiOperation(value = "重新生成报告")
    @PutMapping("/regenerate")
    public CommonResponse<Object> regenerate(@RequestParam(value = "reportDate") String reportDate) {
        runningReportService.regenerate(reportDate);
        return CommonResponse.success("success");
    }

    @ApiOperation(value = "根据客户id查询设备列表")
    @GetMapping("/queryDeviceListByPhone")
    public CommonResponse<List<RunningReportDeviceListResponse>> queryDeviceListByConsumerId(@RequestParam(value = "consumerId") @ApiParam(value = "客户id") Integer consumerId) {

        return CommonResponse.success(runningReportService.queryDeviceListByConsumerId(consumerId));
    }

    @ApiOperation(value = "C端预览查看")
    @GetMapping("/runningReportPreviewDetail")
    public CommonResponse<RunningReportPreviewResponse> runningReportPreviewDetail(@RequestParam(value = "id") Integer id) {

        return CommonResponse.success(runningReportService.runningReportPreviewDetail(id));
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_76)
    @ApiOperation(value = "C端预览编辑")
    @PostMapping("/editRunningReportPreview")
    public CommonResponse<Object> editRunningReportPreview(@RequestBody @Valid RunningReportEditPreviewRequest request) {
        runningReportService.editRunningReportPreview(request);
        return CommonResponse.success("success");
    }

}
